package com.sx.micowell.module.user.writeInfo

import android.app.AlertDialog
import android.app.TimePickerDialog
import android.text.TextUtils
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.mvvm.base.data.DataBindingConfig
import com.example.mvvm.until.GridItemDecoration
import com.example.mvvm.until.LogUtils
import com.example.mvvm.until.ToastUtils
import com.gyf.immersionbar.ImmersionBar
import com.miloyu.modelnet.request.TrainPlanInfo
import com.miloyu.modelnet.response.DictType
import com.sx.micowell.BR
import com.sx.micowell.R
import com.sx.micowell.databinding.ActWriteTargetBinding
import com.sx.micowell.framework.app.IntentKey
import com.sx.micowell.framework.app.MyActivity
import com.sx.micowell.framework.arouth.RoutePath
import com.sx.micowell.framework.model.PhysicalInfoModel

/**
 * @Author: YY
 * @Time: 2021/8/31
 * @Describe 健身目标
 */
@Route(path = RoutePath.WRITE_TARGET)
class WriteTargetActivity : MyActivity<ActWriteTargetBinding>() {

    @Autowired(name = IntentKey.TYPE)
    @JvmField
    var type: String = ""

    @Autowired(name = IntentKey.DATA)
    @JvmField
    var date: String = ""

    @Autowired(name = IntentKey.KEY)
    @JvmField
    var height: String = ""

    @Autowired(name = IntentKey.ID)
    @JvmField
    var initWeight: String = ""

    @Autowired(name = IntentKey.MSG)
    @JvmField
    var targetWeight: String = ""


    private var viewMode: PhysicalInfoModel? = null

    private var trainTypeListAdapter: TrainTypeListAdapter? = null

    override fun getDataBindingConfig(): DataBindingConfig {
        return DataBindingConfig(R.layout.act_write_target, BR.viewModel, viewMode)
    }

    override fun initViewModel() {
        super.initViewModel()
        viewMode = getActivityScopeViewModel(PhysicalInfoModel::class.java)
    }

    override fun initView() {
        ImmersionBar.with(this).statusBarDarkFont(true)
            .fitsSystemWindows(true).init()

        binding.tvComplete.setOnClickListener {
            var targetStr: String?=""
            trainTypeListAdapter?.data?.forEach {
                if (it.isSelect) {
                    targetStr = "${it.dictValue},$targetStr"
                }
            }
            LogUtils.d("tvComplete: ${date}-${type}-${height}-${initWeight}-${targetWeight}-${targetStr}")
            viewMode?.perfectInfo(date, type, height, initWeight, targetWeight, targetStr)
        }
    }

    override fun initData() {
        viewMode?.queryTrainType()
    }

    override fun initViewObservable() {
        super.initViewObservable()
        setTrainTypeList()
    }

    private fun setTrainTypeList() {
        binding.recyclerTarget.isNestedScrollingEnabled = false
        binding.recyclerTarget.layoutManager = GridLayoutManager(context, 3)
        if (binding.recyclerTarget.itemDecorationCount == 0) {
            //添加分割线
            binding.recyclerTarget.addItemDecoration(GridItemDecoration(13, 22))
        }

        viewMode?.trainTypeEvent?.observe(this, {
            trainTypeListAdapter = TrainTypeListAdapter()
            trainTypeListAdapter?.data = it as MutableList<DictType>
            binding.recyclerTarget.adapter = trainTypeListAdapter

            trainTypeListAdapter?.addChildClickViewIds(R.id.imageView1)
            trainTypeListAdapter?.setOnItemChildClickListener { adapter, view, position ->
                val dictType: DictType = it[position]
                dictType.isSelect = !dictType.isSelect
                adapter.notifyItemChanged(position)
            }
        })
    }
}